package models

import (
    "errors"
    orm "go-admin/common/global"
    "golang.org/x/crypto/bcrypt"
    "gorm.io/gorm"
)

type TUser struct {
    gorm.Model
    Username string `json:"username" gorm:"type:varchar(64);comment:Username"` //
    Password string `json:"password" gorm:"type:varchar(128);comment:Password"` // 
    NickName string `json:"nickName" gorm:"type:varchar(128);comment:NickName"` // 
    Phone string `json:"phone" gorm:"type:varchar(11);comment:Phone"` // 
    RoleId string `json:"roleId" gorm:"type:bigint;comment:RoleId"` // 
    Salt string `json:"salt" gorm:"type:varchar(255);comment:Salt"` // 
    Avatar string `json:"avatar" gorm:"type:varchar(255);comment:Avatar"` // 
    Sex string `json:"sex" gorm:"type:varchar(255);comment:Sex"` // 
    Email string `json:"email" gorm:"type:varchar(128);comment:Email"` // 
    Remark string `json:"remark" gorm:"type:varchar(255);comment:Remark"` // 
    Status string `json:"status" gorm:"type:varchar(4);comment:Status"` // 
    StarLevel string `json:"starLevel" gorm:"type:bigint;comment:用户星级"` // 
    CreditScore string `json:"creditScore" gorm:"type:bigint;comment:用户信用分"` // 
    Location string `json:"location" gorm:"type:varchar(1024);comment:IP归属地"` // 
    Ip string `json:"ip" gorm:"type:varchar(64);comment:IP"` // 
}

func (TUser) TableName() string {
    return "t_user"
}

func (e *TUser) GetId() interface{} {
	return e.ID
}

//加密
func (e *TUser) Encrypt() (err error) {
    if e.Password == "" {
        return
    }

    var hash []byte
    if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
        return
    } else {
        e.Password = string(hash)
        return
    }
}

func (e *TUser) Insert() (user TUser, err error) {
    if err = e.Encrypt(); err != nil {
        return
    }

    // check 用户名
    var count int64
    orm.GetDb().Table(e.TableName()).Where("username = ?", e.Username).Count(&count)
    if count > 0 {
        err = errors.New("账户已存在！")
        return
    }

    //添加数据
    if err = orm.GetDb().Table(e.TableName()).Create(&e).Error; err != nil {
        return
    }
    return *e, nil
}

func (e *TUser) Get() (TUser, error) {
    var doc TUser
    table := orm.GetDb().Table(e.TableName())

    if e.ID != 0 {
        table = table.Where("id = ?", e.ID)
    }

    if e.Username != "" {
        table = table.Where("username like ?", "%"+e.Username+"%")
    }

    if err := table.First(&doc).Error; err != nil {
        return doc, err
    }
    return doc, nil
}

func (e *TUser) GetPage(pageSize int, pageIndex int) ([]TUser, int, error) {
    var doc []TUser

    table := orm.GetDb().Table(e.TableName())

    if e.Username != "" {
        table = table.Where("username like ?", "%"+e.Username+"%")
    }

    var count int64

    if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
        return nil, 0, err
    }
    //table.Where("`deleted_at` IS NULL").Count(&count)
    return doc, int(count), nil
}

func (e *TUser) Update(id int) (update TUser, err error) {
    if err = orm.GetDb().Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
        return
    }

    //参数1:是要修改的数据
    //参数2:是修改的数据
    if err = orm.GetDb().Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
        return
    }
    return
}

func (e *TUser) Delete(id int) (success bool, err error) {
    if err = orm.GetDb().Table(e.TableName()).Where("id = ?", id).Delete(&TUser{}).Error; err != nil {
        success = false
        return
    }
    success = true
    return
}

//批量删除
func (e *TUser) BatchDelete(id []int) (Result bool, err error) {
    if err = orm.GetDb().Table(e.TableName()).Where("id in (?)", id).Delete(&TUser{}).Error; err != nil {
        return
    }
    Result = true
    return
}
